<template>
  <div class="cate-container">
    <div class="cate-header">
      <Bread ad="小彭"></Bread>
      <!-- <el-button type="primary" >添加分类</el-button> -->
      <el-button type="primary" @click="dialogFormVisible = true"
        >添加分类</el-button
      >
      <!-- 添加后面的表单 -->
      <el-dialog
        title="添加文章分类"
        :visible.sync="dialogFormVisible"
        @close="close"
      >
        <el-form ref="addRef" :model="form" :rules="addRules">
          <el-form-item
            label="分类名称"
            :label-width="formLabelWidth"
            prop="cate_name"
          >
            <el-input v-model="form.cate_name"></el-input>
          </el-form-item>
          <el-form-item
            label="分类别名"
            :label-width="formLabelWidth"
            prop="cate_alias"
          >
            <el-input v-model="form.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 添加后面表单结束 -->
    </div>
    <!-- 文章分类表单 -->
    <el-table :data="$store.state.article.cateList" style="width: 100%" border>
      <el-table-column label="序号" width="120" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"> </el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <!-- 点击编辑显示表单 -->
          <el-dialog
            title="添加文章分类"
            :visible.sync="dialogFormVisibleEdit"
            @close="close1"
          >
            <el-form ref="addRefinfo" :model="addForm" :rules="addRules">
              <el-form-item
                label="分类名称"
                :label-width="formLabelWidth"
                prop="cate_name"
              >
                <el-input v-model="addForm.cate_name"></el-input>
              </el-form-item>
              <el-form-item
                label="分类别名"
                :label-width="formLabelWidth"
                prop="cate_alias"
              >
                <el-input v-model="addForm.cate_alias"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisibleEdit = false"
                >取 消</el-button
              >
              <el-button type="primary" @click="handleInfo(scope.row)"
                >确 定</el-button
              >
            </div>
          </el-dialog>
          <!-- 点击编辑结束 -->
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 文章分类表单结束 -->
  </div>
</template>

<script>
// 按需引入
import { reqAddArticle, deleateArticle, infoArticle } from '../../api/art.js'
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      dialogFormVisible: false,
      dialogFormVisibleEdit: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      formLabelWidth: '120px',
      // 添加的表单验证
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.articleList()
  },
  methods: {
    ...mapActions('article', ['articleList']),
    // 修改
    handleEdit (row) {
      this.dialogFormVisibleEdit = true
      this.addForm = row
    },
    // 点击确定进行校验,正确输入,提示用户正确
    handleInfo () {
      this.$refs.addRefinfo.validate(async (valid) => {
        if (!valid) return
        // const { data } = await reqAddArticle(this.form)
        const { data } = await infoArticle(this.addForm)
        // 提示用户
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success('新增文章分类成功！')
        // 关闭对话框
        this.dialogFormVisibleEdit = false
        // 重置

        // 重新获取分类列表
        this.articleList()
      })
    },
    // 修改结束
    // 根据id删数据
    handleDelete (id) {
      // 5 点击输入框删除里面的数据---5.1点击后,询问用户是否确定要删除---5.2发起请求,请求数据
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleateArticle(id)
          console.log(res, 111)
          if (res.data.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.articleList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击添加按钮添加
    async handleAdd () {
      // this.$refs.addRef.validate(async (valid) => {
      //   if (!valid) return
      const { data } = await reqAddArticle(this.form)
      // 提示用户
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success('新增文章分类成功！')
      // 关闭对话框
      this.dialogFormVisible = false
      // 重置

      // 重新获取分类列表
      this.articleList()
      // })
      // this.$refs.addRef.resetFields()
    },
    close () {
      /* 重置表单 */
      this.$refs.addRef.resetFields()
    },
    close1 () {
      /* 重置表单 */
      this.$refs.addRefinfo.resetFields()
    }
  }
  // ---
}

// ...mapState('article', ['reqAddArticle'])
</script>

<style>
.cate-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
